import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {nanoid} from 'nanoid'
export class Header extends Component {
    static propTypes = {
        addTodo: PropTypes.func.isRequired
    }

    handlerKeyup = (event) => {
        const {which, target} = event;
        if(which !== 13) {
            return
        }

        if(target.value.trim() === '') {
            return
        }

        const todoObj = {id: nanoid(), name: target.value, done: false}
        this.props.addTodo(todoObj)

        target.value = ''
    }

    render() {
        return (
            <div>
                <input type="text" className="form-control" placeholder="请输入任务的名称，然后按回车键" onKeyUp={this.handlerKeyup}/>
            </div>
        )
    }
}

export default Header
